package com.bot.backend.service.impl.rankList;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bot.backend.mapper.UserMapper;
import com.bot.backend.pojo.User;
import com.bot.backend.service.rankList.GetRankListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GetRankListServiceImpl implements GetRankListService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public JSONObject getList(Integer page) {
        IPage<User> userIPage = new Page<>(page, 10);
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.orderByDesc("rating");
        List<User> records = userMapper.selectPage(userIPage, userQueryWrapper).getRecords();
        for (User user : records) {
            user.setPassword("");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("users", records);
        jsonObject.put("users_count", userMapper.selectCount(null));
        return jsonObject;
    }
}
